//
//  LoginViewController.m
//  Discovery
//
//  Created by HappyMilk 02 on 2017/7/6.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginHeader.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *tf_username;
@property (weak, nonatomic) IBOutlet UITextField *tf_password;

@property (weak, nonatomic) IBOutlet UIButton *btn_login;
@property (weak, nonatomic) IBOutlet UIButton *btn_register;

@property (weak, nonatomic) IBOutlet UIButton *btn_agree;

@end

@implementation LoginViewController

- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [_btn_login setTitle:NSLocalizedStringFromTable(@"Login", @"LoginPlist", nil) forState:UIControlStateNormal];
    [_btn_register setTitle:NSLocalizedStringFromTable(@"Register", @"LoginPlist", nil) forState:UIControlStateNormal];
    [_btn_agree setTitle:NSLocalizedStringFromTable(@"UserAgreement", @"LoginPlist", nil) forState:UIControlStateNormal];
    _tf_username.placeholder = NSLocalizedStringFromTable(@"  Please enter username", @"LoginPlist", nil);
    _tf_password.placeholder = NSLocalizedStringFromTable(@"  Please enter password", @"LoginPlist", nil);
    
    [self createOtherLoginBtn];
}

- (void)createOtherLoginBtn{
    
    if (IS_CHINESE && IS_PASS && IS_IPAD) {
        
        UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){__kwidth/2-40,__kheight/2+100+100,80,80}];
        [btn setImage:[UIImage imageNamed:@"wx_logo.png"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(wxLoginAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }
}
- (void)wxLoginAction{
    QRCodeLoginViewController * qrCodeLoginViewController = [[QRCodeLoginViewController alloc]initWithNibName:@"QRCodeLoginViewController" bundle:nil];
    [self presentViewController:qrCodeLoginViewController animated:true completion:nil];
}

- (IBAction)loginAction:(id)sender {
    
    NSString *username = _tf_username.text;
    NSString *password = _tf_password.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = NSLocalizedStringFromTable(@"Please enter right account and password ,these length must be more than six", @"LoginPlist", nil);
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    //NSLocalizedStringFromTable(@"", @"LoginPlist", nil)
    SHOW_HUD(NSLocalizedStringFromTable(@"Logging...", @"LoginPlist", nil));
    [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
        HIDE_HUD
        if (user != nil) {
            [MPWeChat stander].m_isLogin = true;
            [self dismissViewControllerAnimated:true completion:nil];
            
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:NSLocalizedStringFromTable(@"Hint", @"LoginPlist", nil) message:NSLocalizedStringFromTable(@"Login success", @"LoginPlist", nil) delegate:nil cancelButtonTitle:NSLocalizedStringFromTable(@"Comfirm", @"LoginPlist", nil) otherButtonTitles:nil, nil];
            [alertview show];
            
        } else {
            SHOW_HUD_MESSAGE(NSLocalizedStringFromTable(@"Account or password error", @"LoginPlist", nil));
        }
        
    }];
}
- (IBAction)registerAction:(id)sender {
    
    if (IS_PASS && IS_CHINESE && IS_IPAD) {
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器维护暂时停用注册功能！老用户可正常登录！新用户请使用第三方登录！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        return;
    }
    
    NSString *username = _tf_username.text;
    NSString *password = _tf_password.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = NSLocalizedStringFromTable(@"Please enter right account and password ,these length must be more than six", @"LoginPlist", nil);
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    SHOW_HUD(NSLocalizedStringFromTable(@"Registering...", @"LoginPlist", nil));
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = username;// 设置用户名
    user.password =  password;// 设置密码
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        HIDE_HUD
        if (succeeded) {
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:NSLocalizedStringFromTable(@"Hint", @"LoginPlist", nil) message:NSLocalizedStringFromTable(@"Register success ,please login", @"LoginPlist", nil) delegate:nil cancelButtonTitle:NSLocalizedStringFromTable(@"Comfirm", @"LoginPlist", nil) otherButtonTitles:nil, nil];
            [alertview show];
            
        } else {
            SHOW_HUD_MESSAGE(NSLocalizedStringFromTable(@"login failed", @"LoginPlist", nil));
        }
        
        NSLog(@"%@",error);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [_tf_username resignFirstResponder];
    [_tf_password resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)presentvc:(UIButton *)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"login" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"UserAgreementController"];
    [self presentViewController:vc animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
